home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / curve_isect / vector.h < prev   
Encoding:
C/C++ Source or Header  |  1995-02-06  |  2.1 KB  |  108 lines

  1. // Floating point vector library
  2. #ifndef VECTORS_INCLUDED__
  3. #include <math.h>
  4. class point 
  5.     {
  6.     private:
  7.     int refcount;
  8.     public: 
  9.     double x, y;
  10.     point()
  11.     {
  12.     refcount = 0;
  13.     }
  14.     point( const double _x, const double _y )
  15.     {
  16.     x = _x, y = _y;
  17.     refcount = 1;
  18.     }
  19.     point( const point &p )
  20.     {
  21.     x = p.x, y = p.y; refcount = 1;
  22.     }
  23.     point( const point &p, const int count )
  24.     {
  25.     x = p.x, y = p.y; refcount = count;
  26.     }
  27.     point( const point *a, const point *b, const int count )
  28.     {
  29.     x = ( a->x + b->x ) * 0.5;
  30.     y = ( a->y + b->y ) * 0.5;
  31.     refcount = count;
  32.     }
  33.     friend class Bezier;
  34.     }
  35. ;
  36.  
  37. class vector
  38.     {
  39.     public:
  40.     double x, y;
  41.     vector()
  42.     {
  43.     ;
  44.     }
  45.     vector( const vector &v )
  46.     {
  47.     x = v.x, y = v.y;
  48.     }
  49.     vector( const double _x, const double _y )
  50.     {
  51.     x = _x, y = _y;
  52.     }
  53.     }
  54. ;
  55.  
  56. inline vector operator-(const point a, const point b ) // p - p = v
  57.     {
  58.     return vector( a.x - b.x, a.y - b.y );
  59.     }
  60.     
  61. inline vector operator-(const vector a, const vector b ) // v - v = v
  62.     {
  63.     return vector( a.x - b.x, a.y - b.y );
  64.     }
  65.     
  66. inline point operator+(const point *a, const vector b ) // p + v = p
  67.     {
  68.     return point( a->x + b.x, a->y + b.y );
  69.     }
  70.     
  71. inline point operator+(const point a, const vector b ) // p + v = p
  72.     {
  73.     return point( a.x + b.x, a.y + b.y );
  74.     }
  75.     
  76. inline vector operator+(const vector a, const vector b ) // v + v = v
  77.     {
  78.     return vector( a.x + b.x, a.y + b.y );
  79.     }
  80.     
  81. inline vector operator*(const double s, const vector v ) // sv = v
  82.     {
  83.     return vector( s * v.x, s * v.y );
  84.     }
  85.     
  86. inline vector operator*(const vector v, const double s) // v s = v
  87.     {
  88.     return vector( s * v.x, s * v.y );
  89.     }
  90.     
  91. inline double operator*(const vector a, const vector b) // v * v = s (dot product)
  92.     {
  93.     return a.x * b.x + a.y * b.y;
  94.     }
  95.     
  96. inline point mid( const point *a, const point *b ) 
  97.     {
  98.     return point( ( a->x + b->x ) * 0.5, ( a->y + b->y ) * 0.5 );
  99.     }
  100.     
  101. inline vector vabs( const vector a )
  102.     {
  103.     return vector( fabs( a.x ), fabs( a.y ) );
  104.     }
  105. ;
  106. #define VECTORS_INCLUDED__
  107. #endif
  108.